﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 消息发送
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-08-28 
    /// </summary>
    internal class QYMessageService : DataFactory, QYMessageIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<QYMessageEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<QYMessageEntity>();
                var queryParam = input.queryJson.ToObject();
                //日期范围（近7天、近1月、近3月、自定义）
                if (!queryParam["startTime"].IsEmpty() && !queryParam["endTime"].IsEmpty())
                {
                    var startTime = CommonHelper.GetDateTime(queryParam["startTime"].ToString());
                    var endTime = CommonHelper.GetDateTime(queryParam["endTime"].ToString());
                    startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0, 0);
                    endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999);
                    queryWhere = queryWhere.And(t => t.SendDate >= startTime && t.SendDate <= endTime);
                }
                //关键字
                if (!input.keyword.IsEmpty())
                {
                    var keyword = input.keyword;
                    queryWhere = queryWhere.And(m => m.MsgType.ToString().Contains(keyword) || m.ToUserId.Contains(keyword));
                }
                return await db.GetIQueryable<QYMessageEntity>().Where(queryWhere).OrderByDescending(t => t.SendDate).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(QYMessageEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
    }
}
